package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ResourceBundle;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class LoadFileListener implements ActionListener
{
    ResourceBundle myErrors =
        ResourceBundle.getBundle("resources.errorMessages");
    private static Controller controller;
    private JFileChooser myJFileChooser;


    public LoadFileListener (JFileChooser jfc)
    {
        myJFileChooser = jfc;
    }


    @Override
    public void actionPerformed (ActionEvent e)
    {
        String input = "";
        File selectedFile = myJFileChooser.getSelectedFile();
        try
        {
            Scanner in = new Scanner(selectedFile);
            while (in.hasNext())
            {
                input += in.next();
                input += " ";
            }
            input.trim();
        }
        catch (FileNotFoundException fe)
        {
            String errorFrame = myErrors.getString("ErrorFrame");
            String errorMessage = myErrors.getString("LoadErrorMessage");
            JOptionPane.showMessageDialog(new JFrame(),errorMessage,errorFrame, JOptionPane.ERROR_MESSAGE);
        }
        controller.getModel().processInput(input);
    }


    public static void setController (Controller c)
    {
        controller = c;
    }

}
